﻿// onwer 蒋家伟
#pragma once

// qt
#include <QObject>

// jsdk
#include "ISocket.h"
#include "SocketWorker.h"
#include "ThreadDeleter.h"

class QThread;
namespace jsdk
{
    class Socket final: public QObject, public ISocket
    {
        Q_OBJECT
    public:
        static OwnerPtr<Socket> create(ISocketEventHandler * pSocketEventHandler);
        ~Socket() {
        }

    private:
        // 继承ISocket
        virtual void connected(const std::string& ip, int64_t port) override;
        virtual void send(std::string&& msg, int64_t length) const override;
        virtual void reconnect(const std::string& ip, int64_t port) const override;
        virtual bool isOnLine() const override;

        // owner,用于内部初始化信号与槽
        bool init() const;
        Socket();

    signals:
        void sendMessage(QByteArray, qint64) const;
        void connectSocket(QString, qint64) const;

    private:
        OwnerPtr<QThread, ThreadDeleter> m_opThread;
        OwnerPtr<SocketWorker> m_opSocketWorker;
    };
}